// 连接数据库
const mysql = require('mysql');

const MYSQL_DATABASE = require('../config');

let pool = mysql.createPool({
    host: MYSQL_DATABASE.HOST,
    port: MYSQL_DATABASE.PORT,
    user: MYSQL_DATABASE.USER,
    password: MYSQL_DATABASE.PASSWORD,
    database: MYSQL_DATABASE.DATABASE,
})

// 请求封装
function query(sql, params) {
    console.log(sql, params)
    return new Promise((resolve, reject) => {
        pool.getConnection((err, connection) => {
            if (err) {
                reject(err);
            } else {
                connection.query(sql, params, (err, result) => {
                    connection.release();
                    if (err) {
                        reject(err);
                    } else {
                        resolve(result);
                    }
                })
            }
        })
    })
}

module.exports = {
    query
}